#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_datasource.py 
@version:
@time: 2022/01/05 
"""
from TapClientCore.T02_tm_api.v1_api.v1_tm import V1Connections
from TapClientCore.T03_api_body_templates.v1_api_body import DATASOURCE_HTTP_CUSTOM
from TapClientCore.T03_api_body_templates.v1_api_body import DATASOURCE_HTTP_MQ
from TapClientCore.T03_api_body_templates.v1_api_body import DATASOURCE_HTTP_KAFKA
from TapException.TM import *


class TMFDataSource:

    def __init__(self, change=''):
        self.v1tm = V1Connections(change)

    # 创建
    def _post_connections(self, conf):
        tmp = None
        if conf.get('database_type') == 'kafka':
            tmp = DATASOURCE_HTTP_KAFKA.copy()
            tmp.update(conf)
        elif conf.get('database_type') == 'mq':
            tmp = DATASOURCE_HTTP_MQ.copy()
            tmp.update(conf)
        else:
            tmp = DATASOURCE_HTTP_CUSTOM.copy()
            tmp.update(conf)
        return self.v1tm.post_connections(tmp).get('data')

    def _post_copy_connections(self, connection_id, payload):
        return self.v1tm.post_copy_connections(connection_id, payload)

    # 删除
    def _delete_connections_by_id(self, connections_id):
        return self.v1tm.delete_connections_by_id(connections_id)

    # 修改
    def _patch_connections_by_id(self, connections_id, payload):
        return self.v1tm.patch_connections_by_id(connections_id, payload)

    # 查询
    def _get_connections(self, ):
        return self.v1tm.get_connections().get('data')

    # 查询 by filter
    def _get_connections_by_filter(self, _filter):
        return self.v1tm.get_connections_by_filter(_filter)

    # 查询 by id
    def _get_connections_by_id(self, connection_id):
        return self.v1tm.get_connections_by_id(connection_id)

    def get_id_by_name(self, connection_name):
        response = self.query_datasource_by_name(connection_name)
        connection_id = response.get('id')
        return connection_id

    # 创建datasource by db_info
    def create_datasource(self, conf):
        return self._post_connections(conf)

    # 通过任务名复制
    def copy_datasource_by_name(self, connection_name):
        connection_id = self.get_id_by_name(connection_name)
        payload = {
            "uri": f"{connection_id}/copy",
            "headers":
                {
                    "lconname-name": connection_name
                }
        }
        response = self._post_copy_connections(connection_id, payload).get('data').get('result')
        return response

    # 查询 by name
    def query_datasource_by_name(self, connection_name):
        response = self._get_connections()
        for one in response:
            if connection_name == one.get('name'):
                return one
        from TapUtil.logfunc.logger import logger
        logger.fatal(f'{connection_name} not found')
        raise TapdataNoThisDataSource

    # 删除 by name
    def delete_datasource_by_name(self, connection_name):
        connection_id = self.get_id_by_name(connection_name)
        delete_response = self._delete_connections_by_id(connection_id)
        return delete_response

    # 更新 by name
    def patch_datasource_by_name(self, connection_name, payload):
        connection_id = self.get_id_by_name(connection_name)
        patch_response = self._patch_connections_by_id(connection_id, payload)
        return patch_response

    # 更新 by id
    def patch_datasource_by_id(self, connection_id, payload):
        patch_response = self._patch_connections_by_id(connection_id, payload)
        return patch_response

    # http loading schema by name
    def http_load_schema_by_name(self, connection_name):
        connection_id = self.get_id_by_name(connection_name)
        load = {
            'loadCount': 0,
            "loadFieldsStatus": "loading"
        }
        load_response = self._patch_connections_by_id(connection_id, load)
        return load_response

    # ws loading schema by name
    def ws_load_schema_by_name(self, connection_name):
        response = self.query_datasource_by_name(connection_name)
        from TapClientCore.T05_TM_feature.v1.TMF01_datasource.ws_data_source import ws_load_schema
        ws_result = ws_load_schema(self.v1tm.ws_uri, response)
        if not ws_result:
            raise LoadSchemaError
        return ws_result

    # start testing connections
    def _test_connection_by_name(self, connection_name):
        connection_id = self.get_id_by_name(connection_name)
        test = {
            'status': 'testing'
        }
        self._patch_connections_by_id(connection_id, test)
        return connection_id

    def test_connection_by_name(self, connection_name):
        self._test_connection_by_name(connection_name)
        self.http_load_schema_by_name(connection_name)
        self.ws_load_schema_by_name(connection_name)

    def load_schema_by_name(self, connection_name):
        self.http_load_schema_by_name(connection_name)
        self.ws_load_schema_by_name(connection_name)


if __name__ == '__main__':
    from db_info import Mysql

    T = TMFDataSource()
    res = T.test_connection_by_name('mysql_source_test')
    print(res)
